﻿using System;
using System.Xml;
using Torimi.Wiki.DataObjects;

namespace Torimi.Wiki.Web
{
	/// <summary>
	/// This entire class is for methods and support of things only used during development.
	/// </summary>
	public class Development
	{
		public static void LoadDefaultCategories()
		{
			XmlReaderSettings settings = new XmlReaderSettings();
			settings.ConformanceLevel = ConformanceLevel.Document;
			settings.IgnoreComments = true;
			var dataSourcePath = string.Concat(System.Web.HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Categories.xml");

			using (var reader = XmlReader.Create(dataSourcePath, settings))
			{
				reader.MoveToContent();
				while (reader.ReadToFollowing("Category"))
				{
					var category = new Category
					{
						Id = Guid.NewGuid(),
						Name = reader.GetAttribute("Name"),
						Description = reader.GetAttribute("Description")
					};

					Services.WikiInstance.AddCategory(category);
				}
				reader.Close();
			}
		}

		public static void LoadDefaultWikiPages()
		{
			XmlReaderSettings settings = new XmlReaderSettings();
			settings.ConformanceLevel = ConformanceLevel.Document;
			settings.IgnoreComments = true;
			var dataSourcePath = string.Concat(System.Web.HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\WikiPages.xml");

			using (var reader = XmlReader.Create(dataSourcePath, settings))
			{
				reader.MoveToContent();
				while (reader.ReadToFollowing("WikiPage"))
				{
					var wikiPage = new DataObjects.WikiPage
					{
						Id = Guid.NewGuid(),
						Name = reader.GetAttribute("Name"),
						Title = reader.GetAttribute("Title"),
						Content = reader.ReadInnerXml()
					};

					Services.WikiInstance.AddPage(wikiPage);
				}
				reader.Close();
			}
		}
	}
}
